package cn.gtv.sdk.dcas.api;

import android.content.Context;

import com.sad.jetpack.v1.datamodel.api.IDataModelObtainedCallback;
import com.sad.jetpack.v1.datamodel.api.IDataModelObtainedExceptionListener;
import com.sad.jetpack.v1.datamodel.api.IDataModelRequest;
import com.sad.jetpack.v1.datamodel.api.IDataModelResponse;
import com.sad.jetpack.v1.datamodel.api.utils.LogcatUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

import cn.gtv.sdk.dcas.api.db.Event;

public class DCASDataUploaderImpl implements IDCASDataUploader,IDCASDataUploader.Builder{

    private String tck="dcas_tracker_uploader";
    private Context context;
    private IServerTokenFactory serverTokenFactory= DCASCore.globalConfig.getServerTokenFactory();
    private IDataModelObtainedCallback callback;
    private DCASDataUploaderImpl(Context context){
        this.context=context;
    }

    public static IDCASDataUploader.Builder newBuilder(Context context){
        return new DCASDataUploaderImpl(context);
    }

    @Override
    public void postDCASDataToServer(List<Event> eventList) {
        if (serverTokenFactory!=null){
            serverTokenFactory.onCreateServerToken(new IServerTokenConsumer() {
                @Override
                public void optServerToken(String serverToken) {
                    doPostDCASDataToServer(serverToken,eventList);
                }
            });
        }
    }

    private void doPostDCASDataToServer(String serverToken,List<Event> eventList){

        JSONArray data=new JSONArray();
        try {
            for (Event event:eventList
                 ) {
                String d=event.getData();
                try {
                    JSONObject eventData=new JSONObject(d);
                    data.put(eventData);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            DCASCore.setUploading(context,true);
            HttpSubmitTrackerDataClient.newInstance(context,serverToken)
                    .log(true)
                    .url(DCASCore.getGlobalConfig().getSubmitApi()/*"https://eventlog-api.gzstv.com/data-upload/submit-batch"*/)
                    .params(data.toString())
                    .method(IDataModelRequest.Method.POST)
                    .dataModelProducer(tck)
                    .callback(new IDataModelObtainedCallback<String>() {
                        @Override
                        public void onDataObtainedCompleted(IDataModelResponse<String> iDataModelResponse) {
                            DCASCore.setUploading(context,false);
                            if (callback!=null){
                                callback.onDataObtainedCompleted(iDataModelResponse);
                            }
                        }
                    })
                    .exceptionListener(new IDataModelObtainedExceptionListener() {
                        @Override
                        public void onDataObtainedException(IDataModelRequest iDataModelRequest, Throwable throwable) {
                            DCASCore.setUploading(context,false);
                        }
                    })
                    .execute()
            ;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getTag() {
        return tck;
    }

    @Override
    public IServerTokenFactory getServerTokenFactory() {
        return this.serverTokenFactory;
    }

    @Override
    public IDataModelObtainedCallback getCallback() {
        return this.callback;
    }

    @Override
    public Builder toBuilder() {
        return this;
    }

    @Override
    public Builder tag(String tag) {
        this.tck=tag;
        return this;
    }

    @Override
    public Builder serverFactory(IServerTokenFactory serverTokenFactory) {
        this.serverTokenFactory=serverTokenFactory;
        return this;
    }

    @Override
    public Builder callback(IDataModelObtainedCallback callback) {
        this.callback=callback;
        return this;
    }

    @Override
    public IDCASDataUploader build() {
        return this;
    }
}
